package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.domian.Category;
import com.itheima.reggie.domian.Setmeal;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.SetmealService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private CategoryService categoryService;


    //添加
    @PostMapping
    @CacheEvict(cacheNames = "setmealCache", allEntries = true)
    public R<String> save(@RequestBody SetmealDto setmealDto) {
        setmealService.saveWithDish(setmealDto);
        return R.success("添加成功");
    }

    //分页查询
    @GetMapping("/page")
    public R<Page<SetmealDto>> page(Long page, Long pageSize, String name) {
        //创建page1  将查询到的数据传入
        Page<Setmeal> pageInfo1 = new Page<>(page, pageSize);
        LambdaQueryWrapper<Setmeal> wrap = new LambdaQueryWrapper<>();
        wrap.like(StringUtils.isNotBlank(name), Setmeal::getName, name);
        setmealService.page(pageInfo1, wrap);
        //创建page2  将除了集合内的数据拷贝
        Page<SetmealDto> pageInfo2 = new Page<>();
        BeanUtils.copyProperties(pageInfo1, pageInfo2, "records");
        //得到集合内的每一个setmeal对象,将数据拷贝进dto并进行数据补全
        List<SetmealDto> list = pageInfo1.getRecords().stream().map(setmeal -> {
            //创建dto将数据拷贝进去
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(setmeal, setmealDto);
            //查询分类表
            Category category = categoryService.getById(setmeal.getCategoryId());
            setmealDto.setCategoryName(category.getName());
            return setmealDto;
        }).collect(Collectors.toList());
        pageInfo2.setRecords(list);
        return R.success(pageInfo2);
    }

    //根据多个id批量删除
    @DeleteMapping
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public R<String> delete(Long[] ids) {
        setmealService.deletes(ids);
        return R.success("删除成功");
    }


    //查询
    @GetMapping("/list")
    @Cacheable(cacheNames = "setmealCache", key = "#setmeal.categoryId", unless = "#result.data.size()==0")
    public R<List<Setmeal>> list(Setmeal setmeal) {
        //根据用户传的category_id查询数据库套餐表
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(setmeal.getStatus() != null, Setmeal::getStatus, setmeal.getStatus());
        wrapper.eq(setmeal.getCategoryId() != null, Setmeal::getCategoryId, setmeal.getCategoryId());
        List<Setmeal> list = setmealService.list(wrapper);
        return R.success(list);
    }
}
